﻿#region Using statements
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SmartFrequency.Utils;
using System.IO;
#endregion

namespace SmartFrequency.GUI
{
    public partial class OpenFile : Form
    {
        #region Private variables
        private Language FromLanguage;
        private Settings PSettings;
        private Utilities PUtils = new Utilities();


        private string activeDirectory;
        private string[] filePaths = null;
        private string[] directoryPaths = null;

        private int doubleClickDuration;
        private DateTime doubleClickStart;
        private int lastSelectedIndex = -1;
        private bool showFolders = false;
        #endregion

        #region Public variables
        public string soundFile;
        #endregion

        public OpenFile()
        {
            InitializeComponent();

            this.Text = PUtils.Version;
            PSettings = new Settings("settings.txt");
            PSettings.Load();

            switch (PSettings.GetMember("language"))
            {
                default:
                case "tr":
                case "TR":
                    FromLanguage = new Language(Language.Language_e.TR);
                    label1.Left -= 10;
                    break;
                case "ENG":
                case "eng":
                    FromLanguage = new Language(Language.Language_e.ENG);
                    break;
            }

            // Set date & time values
            try { doubleClickDuration = Convert.ToInt32(PSettings.GetMember("dblclkduration")); }
            catch { doubleClickDuration = PUtils.DOUBLE_CLICK_DURATION; }
            doubleClickStart = DateTime.Now;

            if (Convert.ToInt32(PSettings.GetMember("displaydirectories")) == 1)
                showFolders = true;
            else
                showFolders = false;

            label1.Text = FromLanguage.FrmMsgToString(Language.FormMsg_e.FORM6_HEAD);
            label2.Text = FromLanguage.FrmMsgToString(Language.FormMsg_e.FORM6_LABEL1);
            label3.Text = @"\";
            menuItem1.Text = FromLanguage.FrmMsgToString(Language.FormMsg_e.MENU8_TEXT);
            menuItem2.Text = FromLanguage.FrmMsgToString(Language.FormMsg_e.MENU7_TEXT);

            comboBox1.Items.Clear();
            comboBox1.Items.Add(FromLanguage.FrmMsgToString(Language.FormMsg_e.FORM6_COMBOBOX1));
            comboBox1.Items.Add(FromLanguage.FrmMsgToString(Language.FormMsg_e.FORM6_COMBOBOX2));
            comboBox1.SelectedIndex = 0;

            // Set image list for listview
            listView1.LargeImageList = imageList1;
            // For single item per row
            //listView1.View = View.Details;
            listView1.View = View.LargeIcon;
            listView1.FullRowSelect = true;

            // Start loading files into listview
            LoadFiles(@"\");
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            //Dispose();
            Close();
        }

        private void LoadFiles(string searchDirectory)
        {
            string clearString;

            if (searchDirectory == null)
                return;

            activeDirectory = searchDirectory;
            filePaths = null;
            directoryPaths = null;
            switch (comboBox1.SelectedIndex)
            {
                default:
                case 0:
                    filePaths = Directory.GetFiles(searchDirectory, "*.wav");
                    if(showFolders == true)
                        directoryPaths = Directory.GetDirectories(searchDirectory);
                    break;

                case 1:
                    //string[] filePaths = Directory.GetFiles(@"c:\", "*.*", SearchOption.AllDirectories);
                    filePaths = Directory.GetFiles(searchDirectory, "*.*");
                    if (showFolders == true)
                        directoryPaths = Directory.GetDirectories(searchDirectory);
                    break;
            }

            listView1.Items.Clear();
            listView1.BeginUpdate();

            // Add first entry for "folder up"
            if ((filePaths != null) || (directoryPaths != null))
            {
                ListViewItem listItem1 = new ListViewItem("...");
                listItem1.ImageIndex = 1;
                listView1.Items.Add(listItem1);
            }

            if (activeDirectory == @"\")
                clearString = activeDirectory;
            else
                clearString = activeDirectory + @"\";

            // First list files
            if(filePaths != null)
            {
                foreach (string str in filePaths)
                {
                    //ListViewItem listItem = new ListViewItem(str.Substring(1,str.Length-1));
                    ListViewItem listItem = new ListViewItem(str.Replace(clearString, ""));

                    if(str.EndsWith(".wav") == true)
                        listItem.ImageIndex = 2;
                    else
                        listItem.ImageIndex = 0;

                    listView1.Items.Add(listItem);
                }
            }

            // Then list folders
            if (directoryPaths != null)
            {
                foreach (string str in directoryPaths)
                {
                    //ListViewItem listItem = new ListViewItem(str.Substring(1, str.Length - 1));
                    ListViewItem listItem = new ListViewItem(str.Replace(clearString, ""));
                    listItem.ImageIndex = 1;
                    listView1.Items.Add(listItem);
                }
            }

            listView1.EndUpdate();

            label3.Text = searchDirectory;
        }

        private void comboBox1_Changed(object sender, EventArgs e)
        {
            LoadFiles(activeDirectory);
        }

        private void OpenButton_Click(object sender, EventArgs e)
        {
            int selectedIndex;

            if (listView1.SelectedIndices.Count <= 0)
                return;

            selectedIndex = listView1.SelectedIndices[0];
            if (selectedIndex == 0)
            {
                if (activeDirectory != @"\")
                {
                    int t1;
                    if ((t1 = activeDirectory.LastIndexOf(@"\")) > 0)
                    {
                        LoadFiles(activeDirectory.Substring(0, t1));
                    }
                    else
                    {
                        LoadFiles(@"\");
                    }
                }
            }
            else if (selectedIndex >= filePaths.Length + 1)
            {
                // Folder
                LoadFiles(directoryPaths[selectedIndex - filePaths.Length - 1]);
            }
            else
            {
                // File
                try
                {
                    if (filePaths[selectedIndex - 1].EndsWith(".wav") == true)
                        soundFile = filePaths[selectedIndex - 1];
                    else
                        MessageBox.Show(FromLanguage.ErrMsgToString(Language.FailureMsg_e.ERR_NOTVALIDFILE));
                }
                catch
                {
                    MessageBox.Show(FromLanguage.ErrMsgToString(Language.FailureMsg_e.ERR_NOTVALIDFILE));
                }
            }
        }

        private void listView_ItemActive(object sender, EventArgs e)
        {
            int selectedIndex;
            DateTime temp;

            if (listView1.SelectedIndices.Count <= 0)
                return;

            temp = DateTime.Now;
            selectedIndex = listView1.SelectedIndices[0];

            /*
            if (selectedIndex == lastSelectedIndex)
            {
                TimeSpan TimeDiff = temp - doubleClickStart;
                if ((TimeDiff.Milliseconds < doubleClickDuration) && (TimeDiff.TotalMilliseconds == TimeDiff.Milliseconds))
                {
                    OpenButton_Click(sender, e);
                }
            }
            doubleClickStart = temp;
            lastSelectedIndex = selectedIndex;
            */
            OpenButton_Click(sender, e);
        }

        private void Settings_Click(object sender, EventArgs e)
        {
            FileManagerSettings FManagerSettings = new FileManagerSettings();
            FManagerSettings.Show();
        }

        private void OpenFile_Paint(object sender, PaintEventArgs e)
        {
            PSettings.Load();
            // Set date & time values
            try { doubleClickDuration = Convert.ToInt32(PSettings.GetMember("dblclkduration")); }
            catch { doubleClickDuration = PUtils.DOUBLE_CLICK_DURATION; }
            doubleClickStart = DateTime.Now;

            if (Convert.ToInt32(PSettings.GetMember("displaydirectories")) == 1)
                showFolders = true;
            else
                showFolders = false;

            LoadFiles(activeDirectory);
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            DialogResult ret;
            int selectedIndex;

            if (listView1.SelectedIndices.Count <= 0)
                return;

            selectedIndex = listView1.SelectedIndices[0];
            if (selectedIndex != 0)
            {
                if ((ret = MessageBox.Show(FromLanguage.FrmMsgToString(Language.FormMsg_e.MSGBOX_TEXT2), FromLanguage.FrmMsgToString(Language.FormMsg_e.MSGBOX_TEXT1), MessageBoxButtons.YesNo,
                            MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)) == DialogResult.Yes)
                    try { File.Delete(filePaths[selectedIndex - 1]); }
                    catch { };
            }
        }
    }
}